/*
* Copyright (c) 2007 acmesystems.it - john@acmesystems.it
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*
* Feedback, Bugs...  info@acmesystems.it 
*
*/ 

#include <stdio.h>

#include "libssd1303.h"

void scroll_text(unsigned char *t){
	FILE *fp = fopen(t, "r");
	if(!fp){
		printf("Unknown text file\n");
		return;
	}
	unsigned char text[1024][64];	
	unsigned int i = 0;
	memset(&text[0], 0, 1024 * 64);
	while(!feof(fp)){
		fgets(text[i], 64, fp);
		i++;
	}
	ssd1303_scroll_setup(&text[0][0], i);
	while(1){
		ssd1303_scroll_loop();
		poll(0, 0, 250);
	}
}

void show_image(unsigned char *img){
	FILE *fp = fopen(img, "r");
	if(!fp){
		printf("Unknown image file\n");
		return;
	}
	unsigned char data[128*8];
	fread(data, 128*8, 1, fp);
	fclose(fp);
	ssd1303_image(0, 0, 128, 8, 0, data);
}

int main(int argc, char **argv) {	
	printf("\n\n");
	printf("SSD1303 example application v1.0\n");
	printf("--------------------------------\n\n");
	printf("Made by acmesystems.it (info@acmesystems.it)\n");
	printf("\n");
	
	ssd1303_open();
	
	ssd1303_cls();
	
	
	unsigned char image[256];
	unsigned char text_file[256];
	if(argc != 3){
		printf("Invalid argument\n");
		return 1;
	}
	switch(*argv[1]){
	case 't':
		scroll_text(argv[2]);	
		break;
	case 'i':
		show_image(argv[2]);
		break;

	}
	
	ssd1303_close();
		
	return 0;
}




